define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'app/topic/index' + location.search,
                    // add_url: 'app/topic/add',
                    edit_url: 'app/topic/edit',
                    // refreshdata_url: 'app/topic/refreshdata',
                    // del_url: 'app/topic/del',
                    multi_url: 'app/topic/multi',
                    import_url: 'app/topic/import',
                    table: 'app_topic',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                // sortName: 'ID',
                sortName: 'createtime',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name')},
                        {field: 'countisr', title: __('Countisr'), operate:false},
                        {field: 'countpv', title: __('Countpv'), operate:false},
                        {field: 'countshare', title: __('Countshare'), operate:false},
                        {field: 'countpraise', title: __('Countpraise'), operate:false},
                        {field: 'countcomment', title: __('Countcomment'), operate:false},
                        {field: 'twodayspv', title: __('Twodayspv'), operate:false},
                        {field: 'twodaysshare', title: __('Twodaysshare'), operate:false},
                        {field: 'twodayspraise', title: __('Twodayspraise'), operate:false},
                        {field: 'twodayscomment', title: __('Twodayscomment'), operate:false},
                        {field: 'hotswitch', title: __('Hotswitch'), searchList: {"0":__('Hotswitch 0'),"1":__('Hotswitch 1')}, table: table, formatter: Table.api.formatter.toggle},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 绑定btn-refreshdata按钮点击事件
            $('.btn-refreshdata').on("click",function(){
                // alert(1);
                Fast.api.ajax({
                    url: 'app/topic/refreshdata',
                    type: 'get',
                    data: {
                        // 模拟Token MD5加密
                        // 0759Home.com
                        // 246a99a1e55346d39c44a2a086a213ad
                        token: '246a99a1e55346d39c44a2a086a213ad'
                    }
                }, function(data, ret){
                    // 成功的回调
                    // console.log(data)
                    // console.log(ret)
                    if( ret.code ){
                        table.bootstrapTable('refresh');
                    }
                    // console.log(1)
                    // return false
                })
            })

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));

            }
        }
    };
    return Controller;
});
